package business.service.card.impl;

import java.sql.SQLException;
import java.util.List;

import business.entity.Card;
import business.service.card.ICardListViewService;

import app.MyApplication;

import ui.activity.card.CardListActivity;
import ui.model.card.CardListModel;
import util.data.carddao.ICardDataContext;


public class CardListViewService implements ICardListViewService {
	
	private CardListModel viewModel;
	// private IDataContext dct = null;
	private ICardDataContext dct = null;

	/**
	 * @param v
	 *            传入ViewModel
	 */
	public CardListViewService(CardListModel v, CardListActivity activity) {
		viewModel = v;
		// dct = new DataContext();
		dct = MyApplication.cardDataContext;
	}

	@Override
	public boolean search(String name) {
		List<Card> resultList;
		try {
			resultList = dct.queryByName(name);
			viewModel.setCardList(resultList);
//			activity.onRefresh();
			
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean listAllCard() {
		List<Card> list;
		try {
			list = dct.queryForAll();
			viewModel.setCardList(list);
//			activity.onRefresh();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Card search(int id) {
		Card card;
		try {
			card = dct.queryById(id);
			return card;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

}
